﻿using Aspose.BarCode.BarCodeRecognition;
using Aspose.BarCode.Generation;
using System;
using System.Collections.Generic;
using System.Text;

namespace TL_Cloud.Utils
{
    public class BarCodeHelper
    {
        static BarCodeHelper()
        {
            Aspose.BarCode.License license = new Aspose.BarCode.License();
            if (license.IsLicensed == false)
            {
                license.SetLicense(AsposeLicense.LStream);
            }
        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        public static void MakeQRCode(string codeText,string saveFilePath)
        {
            using (BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.QR, codeText))
            {
                generator.XDimension.Pixels = 3;
                generator.Margins.Left.Pixels = 20;
                generator.Margins.Right.Pixels = 20;
                generator.Save(saveFilePath);
            }
        }

        /// <summary>
        /// 解析二维码的内容
        /// </summary>
        /// <param name="picPath"></param>
        /// <returns></returns>
        public static string ParseQR(string picPath)
        {
            string content = "";
            using (BarCodeReader reader = new BarCodeReader(picPath, DecodeType.QR))
            {
                while (reader.Read())
                {
                    content += reader.GetCodeText();
                }
            }
            return content;
        }
    }
}
